﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FirstGame.Objects
{
    public class Enemy : MovingObject
    {
        private float _speed = 3f;
        
        public Enemy(Texture2D sprite)
        {
            Sprite = sprite;
        }

        /// <summary>
        /// Quadrant 1 - Top of screen
        /// Quadrant 2 - Right side of screen
        /// Quadrant 3 - Bottom of screen
        /// Quadrant 4 - Left side of screen
        /// </summary>
        /// <param name="screenX"></param>
        /// <param name="screenY"></param>
        public void Spawn(float screenX, float screenY)
        {
            int intScreenX = Convert.ToInt32(screenX);
            int intScreenY = Convert.ToInt32(screenY);
            Coords spawnCoords = new Coords(0,0);
            float xSpeed = 0f;
            float ySpeed = 0f;
            Random random = new Random();
            switch (random.Next(4))
            {
                case 0 :
                    spawnCoords = new Coords(random.Next(intScreenX - Sprite.Width), -Sprite.Height);
                    ySpeed = _speed;
                    break;
                case 1 :
                    spawnCoords = new Coords(intScreenX, random.Next(intScreenY - Sprite.Height));
                    xSpeed = -_speed;
                    break;
                case 2 :
                    spawnCoords = new Coords(random.Next(intScreenX - Sprite.Width), intScreenY + Sprite.Height);
                    ySpeed = -_speed;
                    break;
                case 3 :
                    spawnCoords = new Coords(-Sprite.Width, random.Next(intScreenY - Sprite.Height));
                    xSpeed = _speed;
                    break;
            }
            Position = new Vector2(spawnCoords.X, spawnCoords.Y);
            Speed = new Vector2(xSpeed, ySpeed);
            SetBoundingBox();
        }

        public bool IsEnemyOutOfBounds(Coords min, Coords max)
        {
            UpdateObject();
            return Position.X > max.X + Sprite.Width + 1
                   || Position.X < min.X - Sprite.Width - 1
                   || Position.Y > max.Y + Sprite.Height + 1
                   || Position.Y < min.Y - Sprite.Height -1;
        }
    }
}
